|
|
|
|
|
|
|
Dim i As Integer
i=1
While i <= 100
Debug.Print "Count=" & i
Wend
End |
|
|
|
|
|
|
|
|
Run the program, and you should immediately spot the bug, which causes the same value to be printed over and over again. As a matter of fact, the erroneous While loop will never stop. |
|
|
|
|
|
|
|
|
When you are working in Visual Basic's IDE, you know that you can click the Stop button to end the program, fix the code, and then restart the application. However, often you might want to debug your programs while they are executing. To do so, you need to use the Break button on Visual Basic's Debug toolbar, or press Ctrl+Break. If you are still running the sample program, press Ctrl+Break now. Notice that code execution stops (the numbers in the Immediate window are no longer printing), but your program is not completely stopped. The forms are still loaded, and the variables still contain their contents. You can press F5 or click the VB Start button to resume execution. If you display the Code window while the program is paused, Visual Basic highlights the next statement and places an arrow indicator in the margin of the Code window. |
|
|
|
|
|
|
|
|
This paused state of execution is known as break mode. While you are in break mode, you can view or change variable values in the Immediate window. In some cases, you can even modify the code and continue execution. |
|
|
|
|
| |
|
|
|
|
The easiest way to know if you are in break mode is to look at Visual Basic's caption, which will have the word [break] in it. You can also look at the buttons on the toolbar, or try to type in the Immediate window. |
|
|
|
|
|
|
|
|
|
|
|
Correcting errors like this one before compiling the program is best. Because users are running outside the Visual Basic IDE, they have no way of pausing or ending an infinite loop other than killing the task from the Windows Task Manager.  |
|
|
|
|
|
|
|
|
|
|
In the example, you obviously "forgot" a statement to increment the counter variable i. Because the value of i never changes, the While condition will never become False. To fix this error, while you are still in break mode, modify the code as follows to include the missing line: |
|
|
|
|
|
|
|
|
Dim i As Integer
i=1
While i <= 100
Debug.Print "Count=" & i
i = i + 1
Wend |
|
|
|
|
|
|
|
|
Next, press F5 or click the VB Start button after adding the statement, and the program should count up to 100 and then stop. |
|
|
|
|
|